import { ref } from 'vue'
import { adapter, request } from '@/request'
import { priceListByGet } from '@/core/api/priceSheet'
import type { ComboPriceTypeEnum } from '@/core/enums/type'
import type { RequestWrapper } from '@/core/interfaces/common'
import type { Combo, Price } from '@/core/interfaces/combo'
import { useMainStore } from '@/store/useMainStore'
const mainStore = useMainStore()
interface Params {
  comboId: Combo['comboId']
  platformIds?: string
}

export interface ComboPrice {
  comboId: Combo['comboId']
  id: Price['comboPriceId']
  info: Price['comboPriceName']
  price: number // 特惠价，使用 premiumPrice
  premiumPrice: number
  premiumRatio: number
  marketPrice: number
  originalPrice: number
  premiumDiscountPrice: number
  zijianPrice: number
  schedulingTimes: number
  type: ComboPriceTypeEnum
  label: Price['comboPriceName']
  value: Price['comboPriceId']
}

export default function getComboPriceList() {
  const service = adapter<RequestWrapper<ComboPrice[]>>(request)
  const loading = ref(false)
  const response = ref<ComboPrice[]>([])

  const fetch = (params: Params) => {
    const { url, method } = priceListByGet()
    loading.value = true

    return service({
      config: {
        url,
        method,
        params
      },
      callback: (res) => {
        mainStore.pricecibaos(res.data.data[0].premiumPrice)

        response.value = res.data.data.map((item) => {
          return {
            ...item,
            label: item.info,
            value: item.id,
            // 特惠价使用 premiumPrice
            price: item.premiumPrice,
            // 使用接口返回的实际字段
            premiumPrice: item.premiumPrice,
            premiumRatio: item.premiumRatio,
            marketPrice: item.price,
            originalPrice: item.originalPrice,
            premiumDiscountPrice: item.premiumDiscountPrice,
            zijianPrice: item.zijianPrice
          }
        })
      },
      error: (err) => {
        console.log(err)
      },
      done: () => {
        loading.value = false
      }
    })
  }

  return [response, fetch, loading] as const
}
